package com.qf.utils;

import com.qf.service.AccountService;

import java.sql.SQLException;

/**
 * @Description: 公用常量
 * @Data: 2022/9/1 9:00
 * @Pacakge: com.qf.utils
 * @ClassName: TransactionManager
 * @Author: fan
 * 事务管理
 */
public class TransactionManager {
    private ConnectionUtils connectionUtils;
    //set注入
    public void setConnectionUtils(ConnectionUtils connectionUtils) {
        this.connectionUtils = connectionUtils;
    }
    //开启事务管理
    public void beginTransaction(){
        try {
            connectionUtils.getConnection().setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //提交事务
    public void commitTransaction(){
        try {
            connectionUtils.getConnection().commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //事务回滚
    public void rollbackTransaction(){
        try {
            connectionUtils.getConnection().rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    //释放资源
    public void release(){
        connectionUtils.removeConnection();
    }
}
